import http from '@/config/axios-config'
import { AxiosPromise } from 'axios'

/**
 * 展示目录
 * @param name 项目名称
 * @returns 
 */
export function showDirectory(name: string): AxiosPromise {
  return http({
    url: `/api/v1/file/show_directory/${name}`,
    method: 'get'
  })
}

/**
 * 发起请求获得文件内容
 * @param path 虚拟路径
 */
export function getFile(path: string): AxiosPromise {
  return http({
    url: `/api/v1/file/get_file${path}`,
    method: 'get'
  })
}

/**
 * 创建文件
 * @param path 虚拟路径
 * @param name 文件名
 */
export function createFile(path: string, name: string) {
  const idx = path.lastIndexOf('.')
  let ext = ''
  if (idx != -1) {
    ext = path.substring(idx + 1)
  }
  return http({
    url: '/api/v1/file/create_file',
    method: 'post',
    headers: {
      'Content-Type': 'application/json'
    },
    data: {
      path: path,
      name: name,
      extension: ext
    }
  })
}

/**
 * 创建文件夹
 * @param path 虚拟路径
 * @param name 文件名
 */
 export function createDirectory(path: string, name: string) {
  return http({
    url: '/api/v1/file/create_directory',
    method: 'post',
    headers: {
      'Content-Type': 'application/json'
    },
    data: {
      path: path,
      name: name
    }
  })
}

/**
 * 删除指定路径的全部
 * @param path 虚拟路径
 * @param name 文件/目录名
 * @returns 
 */
export function deleteAll(path: string, name: string, isRoot: boolean) {
  const idx = path.lastIndexOf('.')
  let ext = ''
  if (idx != -1) {
    ext = path.substring(idx + 1)
  }
  return http({
    url: '/api/v1/file/delete_all',
    method: 'post',
    headers: {
      'Content-Type': 'application/json'
    },
    data: {
      path: path,
      name: name,
      extension: ext,
      isRoot
    }
  })
}

/**
 * 重命名
 * @param oldPath 旧路径
 * @param newPath 新路径
 */
export function rename(oldPath: string, newPath: string, isRoot: boolean) {
  return http({
    url: '/api/v1/file/rename',
    method: 'post',
    headers: {
      'Content-Type': 'application/json'
    },
    data: {
      oldPath,
      newPath,
      isRoot
    }
  })
}

/**
 * 保存代码内容
 * @param path 路径
 * @param code 内容
 */
export function save(path: string, code: string) {
  return http({
    url: '/api/v1/file/save',
    method: 'post',
    headers: {
      'Content-Type': 'application/json'
    },
    data: {
      path,
      content: code
    }
  })
}

/**
 * 登录
 * @param username 账号
 * @param password 密码
 * @returns 
 */
export function login(username: string, password: string) {
  return http({
    url: '/api/v1/user/login',
    method: 'post',
    headers: {
      'Content-Type': 'application/json'
    },
    data: {
      username,
      password
    }
  })
}

/**
 * 注册
 * @param username 账号
 * @param password 密码
 * @returns 
 */
export function register(username: string, password: string) {
  return http({
    url: '/api/v1/user/register',
    method: 'post',
    headers: {
      'Content-Type': 'application/json'
    },
    data: {
      username,
      password
    }
  })
}

/**
 * 查询所有语言相关信息
 */
export function selectLangAll() {
  return http({
    url: '/api/v1/lang/select_all',
    method: 'get'
  })
}

/**
 * 查询所有构建工具信息
 * @returns 
 */
export function selectBuildTools() {
  return http({
    url: '/api/v1/pro/select_build_tools',
    method: 'get'
  })
}

/**
 * 创建项目
 * @param langId 编程语言
 * @param langVersionId 语言版本
 * @param buildToolId 依赖管理工具
 * @param projectName 项目名称
 * @returns 
 */
export function createProject(langId: number, langVersionId: number, buildToolId: number | null, projectName: string, groupId: string, artifactId: string, version: string) {
  return http({
    url: '/api/v1/pro/create',
    method: 'post',
    headers: {
      'Content-Type': 'application/json'
    },
    data: {
      langId,
      langVersionId,
      buildToolId,
      projectName,
      groupId,
      artifactId,
      version
    }
  })
}

/**
 * 查询自己所有项目
 * @returns 
 */
export function selectAllProject() {
  return http({
    url: '/api/v1/pro/select_all',
    method: 'get'
  })
}

/**
 * 根据项目id拿到websocket相关信息
 * @returns 
 */
export function getWebsocket(pid: number) {
  return http({
    url: `/api/v1/pro/get_websocket/${pid}`,
    method: 'get'
  })
}